package com.sm.tool.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;

import java.util.*;

/**
 * @program: practice
 * @description:
 * @author: liqitian.
 * @create: 2019-11-20 17:31
 **/
@Configuration
public class RedisConfig {
    @Bean
    public JedisCluster redisCluster(){
        List<String> list=new ArrayList<>();
        list.add("127.0.0.1:7001");
        list.add("127.0.0.1:7002");
        list.add("127.0.0.1:7003");
        list.add("127.0.0.1:7004");
        list.add("127.0.0.1:7005");
        list.add("127.0.0.1:7006");

        Set<HostAndPort> nodes = new HashSet<>();
        for (String node:list){
            String[] parts= StringUtils.split(node,":");
            Assert.state(parts.length==2, "redis node shoule be defined as 'host:port', not '" + Arrays.toString(parts) + "'");
            nodes.add(new HostAndPort(parts[0], Integer.valueOf(parts[1])));
        }

        return new JedisCluster(nodes);
    }
}
